// 请求库
import  md5  from '../utils/md5';
//const host = 'http://192.168.0.104:8808'; 
const host = 'https://bigerfe.com'; 

const querySerialize = (opt,passArr)=> {
  opt || (opt = {});
  passArr=passArr || [];
  const keys = Object.keys(opt);
  let str = '';
  keys.forEach(item => {
      if(passArr.indexOf(item)===-1){
          str += `${item}=${opt[item]}&`
      }
  });

  return str;
}
const getStatus = ()=>{
  //out
  return 'abc789kl...';
}

const geSignStr = (opt,passArr)=>{
  var str = querySerialize(opt,passArr);
  var key = getStatus();
  var value = key+str+key.split('').reverse();
  var sign = md5(value);
  return sign.toString();
}

const baseRequest = (url,data)=>{
  data = data || {};
  data.t = +new Date();
  url = host + url;
  const sign = geSignStr(data,['sign']);
  return new Promise((resolve,reject) => {
    wx.request({
      url,
      data: {
        ...data,
        sign
      },
      method:'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        const {code, msg, data} = res.data || {};
        if(code !==0){
          wx.showToast({
            title: msg || '数据异常，请稍后再试~',
            icon:'none'
          })
          resolve(null);
          return;
        }
        resolve(data);
      },
      fail(res) {
        resolve(-1);
      }
    })
  })
}
export default baseRequest